package com.boyinet.imesh2json.common

import java.lang.StringBuilder
import java.util.*

/**
 * @author lengchunyun
 */
object ModbusStrBuilder {
    /**
     * 对不足规定长度的字符串填零补位
     *
     * @param str       要补齐的原始字符串
     * @param strLength 补齐后的长度
     * @return 补齐后的字符串
     */
    fun addZeroForNum(str: String, strLength: Int): String {
        var temp = str
        var strLen = temp.length
        if (strLen < strLength) {
            val strBuilder = StringBuilder(temp)
            while (strLen < strLength) {
                // sb.append(str).append("0");//右补0
                // 左补0
                strBuilder.insert(0, "0")
                strLen = strBuilder.length
            }
            temp = strBuilder.toString()
        } else {
            temp = temp.substring(strLen - strLength)
        }
        return temp.uppercase(Locale.getDefault())
    }
}